<template>
	<view class="container">
		<view class="title">信用卡账户信息</view>
		
		<view class="card">
			<view class="card-item">
				<text class="label">审批额度</text>
				<text class="content">{{account.approvedLimit|MoneyFormat}}</text>
			</view>
			
			<view class="card-item">
				<text class="label">取现额度</text>
				<text class="content">{{account.cashadvanceLimit|MoneyFormat}}</text>
			</view>
			
			<view class="card-item">
				<text class="label">已用额度</text>
				<text class="content">{{account.usedLimit|MoneyFormat}}</text>
			</view>
			
			<view class="card-item">
				<text class="label">账户金额</text>
				<text class="content">{{account.availableLimit|MoneyFormat}}</text>
			</view>						
		</view>
		
		<button type="primary" v-if="!isBind" @click="bindCard">绑定当前账户到电子钱包</button>
		<button type="warn" v-if="isBind" @click="cancelBindCard">取消绑定当前账户</button>		
	</view>
</template>

<script>
	import Global from '../../common/global.js'
	
	export default {
		data() {
			return {
				customerNumber:'',
				accountNumber:'',
				account: {},
				accountType:'CRED',
				isBind:false
			}
		},
		methods: {
			//查询当前账户信息
			getCardDetail(){
				uni.showLoading({
				    title: '正在读取'
				});
				
				uni.request({
					url: Global.BASE_URL + '/ewallet/creditLimitDetails',
					header:{
						'content-type': 'application/x-www-form-urlencoded'
					},
					data:{
						customerNumber:this.customerNumber,
						accountNumber:this.accountNumber
					},
					method:'POST',
					success:(res)=>{ //注意使用箭头函数						
					
						//账户信息
						this.account = res.data
												
						console.log(res)						
					},
					fail(err) {
						uni.showToast({
							title:'查询信用卡账户信息接口调用失败',
							icon:'none'
						})
					},
					complete() {
						uni.hideLoading()
					}
				})				
			},
			//查询当前卡是否绑定信息
			isBindCard(){
				uni.request({
					url: Global.BASE_URL + '/ewallet/queryBindAccount',
					header:{
						'content-type': 'application/x-www-form-urlencoded'
					},
					data:{
						customerNumber:this.customerNumber,
						accountNumber:this.accountNumber
					},
					method:'POST',
					success:(res)=>{ //注意使用箭头函数						
						
						if (res.data == null || res.data == '') {
							this.isBind = false
						} else {
							this.isBind = true
						}
												
						console.log("res=",res)						
					},
					fail(err) {
						uni.showToast({
							title:'查询账户信息接口调用失败',
							icon:'none'
						})
					}
				})
			},
			//绑卡操作
			bindCard(){
				uni.showModal({
					title: '绑卡提示',
					content: '要将' + this.accountNumber + '绑定到电子钱包吗' ,
					success: (res)=> {
						if (res.confirm) {
							
							uni.request({
								url: Global.BASE_URL + '/ewallet/bindAccount',
								header:{
									'content-type': 'application/x-www-form-urlencoded'
								},
								data:{
									customerNumber:this.customerNumber,
									accountNumber:this.accountNumber,
									accountType:this.accountType
								},
								method:'POST',
								success:(res)=>{ //注意使用箭头函数						
									uni.showToast({
										title:'绑卡成功',
										duration: 1000
									})
									
									setTimeout(()=>{
										uni.redirectTo({
											url:'../bind-card/bind-card'
										})
									},1000);
								},
								fail(err) {
									uni.showToast({
										title:'绑卡接口调用失败',
										icon:'none'
									})
								}
							})
							
						} 
					}
				});				
			},
			//取消绑卡操作			
			cancelBindCard(){
				uni.showModal({
					title: '取消绑卡提示',
					content: '要将' + this.accountNumber + '取消绑定吗' ,
					success: (res)=> {
						if (res.confirm) {
							
							uni.request({
								url: Global.BASE_URL + '/ewallet/cancelBindAccount',
								header:{
									'content-type': 'application/x-www-form-urlencoded'
								},
								data:{
									customerNumber:this.customerNumber,
									accountNumber:this.accountNumber,
									accountType:this.accountType
								},
								method:'POST',
								success:(res)=>{ //注意使用箭头函数						
									uni.showToast({
										title:'取消绑卡成功',
										duration: 1000
									})
									
									setTimeout(()=>{
										uni.redirectTo({
											url:'../bind-card/bind-card'
										})
									},1000);
								},
								fail(err) {
									uni.showToast({
										title:'取消绑卡接口调用失败',
										icon:'none'
									})
								}
							})
							
						} 
					}
				});				
			}
		},
		onLoad(options) {
			//获得账号
			this.accountNumber = options.accountNumber;			
			//读取出客户编号
			this.customerNumber = uni.getStorageSync('customerNumber')
			
			//查询当前账户信息			
			this.getCardDetail()
			//查询当前卡是否绑定信息
			this.isBindCard()
		}
	}
</script>

<style>
	page {
		background: #EDEDED;
	}
	
	.container {
		padding: 15rpx;
		color: #959595;
	}
	
	.title {
		color: #333333;
		font-size: 30rpx;		
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	
	.card {		
		font-size: 30rpx;		
		background-color: #FFFFFF;
		/* border-radius: 25rpx; */
		margin-bottom: 30rpx;
	}
		
	.card .card-item {
		display: flex;
		justify-content: left;
		min-height: 70rpx;
		line-height: 70rpx;
		border-bottom: 1px solid #eeeeee;
		padding-left: 30rpx;
	}
	
	.card .label {		
		width: 200rpx;
	}
	
	.card .content {
		color: #007AFF;	
		width: 520rpx;
	}
			
	button {
		margin-top: 20rpx;
	}
</style>